package com.travel.modules.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.travel.commons.utils.Constant;
import com.travel.commons.utils.R;
import com.travel.modules.sys.dto.SysConfigDTO;
import com.travel.modules.sys.dto.SysConfigSafeDTO;
import com.travel.modules.sys.entity.SysConfig;
import com.travel.modules.sys.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@Slf4j
@RestController
@RequestMapping("/sys/setting")
public class SysSettingController {
    @Resource
    DataSource dataSource;

    @Resource
    private SysConfigService sysConfigService;

    /**
     * 恢复出厂设置
     */
    @RequestMapping("/reset")
    public R reset() {
        Connection connection;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            log.error(e.getMessage());
            return R.error("执行失败");
        }
        if (connection == null) {
            return R.error("执行失败");
        }
        ClassPathResource cr = new ClassPathResource("classpath:sql/reset.sql");
        EncodedResource er = new EncodedResource(cr, "utf-8");
        ScriptUtils.executeSqlScript(connection, er);
        try {
            connection.close();
        } catch (SQLException e) {
            log.error(e.getMessage());
            R.error("执行完成后关闭连接异常");
        }
        return R.ok();
    }

    @RequestMapping("/base")
    public R base(@RequestBody SysConfigDTO form) {
        boolean isUpdate = false;
        SysConfigSafeDTO safeDTO = sysConfigService.getConfigObject(Constant.SYSTEM_CONFIG_SAFE, SysConfigSafeDTO.class);
        if (form.getValid() != null) {
            safeDTO.getPassword().setValid(form.getValid());
            isUpdate = true;
        }
        if (form.getFail() != null) {
            safeDTO.getLogin().setFail(form.getFail());
            isUpdate = true;
        }
        if (form.getLock() != null) {
            safeDTO.getLogin().setLock(form.getLock());
            isUpdate = true;
        }
        if (isUpdate) {
            SysConfig sysConfig = sysConfigService.getOne(new QueryWrapper<SysConfig>().eq("config_key", Constant.SYSTEM_CONFIG_SAFE));
            sysConfig.setConfigValue(JSONObject.toJSONString(safeDTO));
            sysConfigService.updateById(sysConfig);
        }

        if (form.getOrderExpire() != null) {
            SysConfig orderConfig = sysConfigService.getOne(new QueryWrapper<SysConfig>().eq("config_key", Constant.SYSTEM_CONFIG_ORDER_EXPIRE));
            orderConfig.setConfigValue(form.getOrderExpire().toString());
            sysConfigService.updateById(orderConfig);
        }
        return R.ok();
    }

    @RequestMapping("/base/info")
    public R baseInfo() {
        SysConfigSafeDTO safeDTO = sysConfigService.getConfigObject(Constant.SYSTEM_CONFIG_SAFE, SysConfigSafeDTO.class);
        SysConfigDTO sysConfigDTO = new SysConfigDTO();
        sysConfigDTO.setValid(safeDTO.getPassword().getValid());
        sysConfigDTO.setFail(safeDTO.getLogin().getFail());
        sysConfigDTO.setLock(safeDTO.getLogin().getLock());
        sysConfigDTO.setOrderExpire(Integer.parseInt(sysConfigService.getOne(new QueryWrapper<SysConfig>().eq("config_key", Constant.SYSTEM_CONFIG_ORDER_EXPIRE)).getConfigValue()));
        return R.ok().put("page", sysConfigDTO);
    }
    
    /**
     * 备份
     */
    @RequestMapping("/backup/execute")
    public R backup() {
        Connection connection;

        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            log.error(e.getMessage());
            return R.error("执行失败");
        }
        if (connection == null) {
            return R.error("执行失败");
        }
        /* 备份 */
        try {
            connection.close();
        } catch (SQLException e) {
            log.error(e.getMessage());
            return R.error("执行完成后关闭连接异常");
        }
        return R.ok();
    }

    /**
     * 备份列表
     */
    @RequestMapping("/backup/list")
    public R backupList(@RequestBody SysConfig form) {
        return R.ok();
    }

    /**
     * 删除备份
     */
    @RequestMapping("/backup/del")
    public R backupDelete() {
        return R.ok();
    }

    /**
     * 恢复备份
     */
    @RequestMapping("/backup/resume")
    public R backupResume() {
        return R.ok();
    }
}
